package cn.myfirefly.moming.house.dao;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.house.dao.mapper.SpfTradeContractMapper;
import cn.myfirefly.moming.house.entity.SpfTradeContract;
import cn.myfirefly.moming.house.model.SpfTradeContractDetailDto;
import cn.myfirefly.moming.house.model.SpfTradeContractDto;
import cn.myfirefly.moming.house.model.SpfTradeContractFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.mybatis.BjuiDataGridFilter;
import cn.myfirefly.core.mybatis.BjuiDataGridSqlHelper;

@Repository
public class SpfTradeContractDao extends BaseDao<SpfTradeContract> {
	
	@Autowired
	private SpfTradeContractMapper spfTradeContractMapper;
	
	public PageResultDto<SpfTradeContractDto> getList(SpfTradeContractFilterDto dto) {
		PageResultDto<SpfTradeContractDto> resultDto = new PageResultDto<>();
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		//分页处理
		Page<SpfTradeContractDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<SpfTradeContractDto> list = spfTradeContractMapper.getList(paraMap);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
	public PageResultDto<SpfTradeContractDto> getCommissionList(SpfTradeContractFilterDto dto) {
		PageResultDto<SpfTradeContractDto> resultDto = new PageResultDto<>();
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		//分页处理
		Page<SpfTradeContractDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<SpfTradeContractDto> list = spfTradeContractMapper.getListForCommission(paraMap);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
	public List<SpfTradeContractDto> getCommissionAllList(SpfTradeContractFilterDto dto) {
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		paraMap.putAll(bjuiDataGridFilter.getParam());
		List<SpfTradeContractDto> list = spfTradeContractMapper.getListForCommission(paraMap);
		return list;
	}
	
	public BigDecimal getTotalAmt(SpfTradeContractFilterDto dto) {
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto, "x");
		paraMap.putAll(bjuiDataGridFilter.getParam());
		BigDecimal totalPerformance = spfTradeContractMapper.getTotalAmt(paraMap);
		return totalPerformance;
	}
	
	public Map<String, BigDecimal> getTotalCommission(SpfTradeContractFilterDto dto) {
		//参数处理
		Map<String, Object> paraMap = new HashMap<>();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto, "x");
		paraMap.putAll(bjuiDataGridFilter.getParam());
		List<Map<String, BigDecimal>> list = spfTradeContractMapper.getTotalCommission(paraMap);
		return list.get(0);
	}
	
	public SpfTradeContractDetailDto getContractDetail(Integer id) {
		return spfTradeContractMapper.getContractDetail(id);
	}
}
